/*
 * @(#)ControllerPanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.netexplode.jtunes.client.player.jTunes;
import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.player.PlayerException;
import com.netexplode.jtunes.model.player.PlayerManager;

/**
 * <code>ControllerPanel</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.6 $
 * @since 0.1
 */
public class ControllerPanel extends JPanel {

    // private JPanel controlPanel;

    private static final VolumeControl volumeControl = new VolumeControl();

    private final static Dimension MAX_DIMENSION = new Dimension(250, 60);

    public ControllerPanel() {
        super();
        initialize();
    }

    private void initialize() {
        super.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

        // adding subpanes to control pane
        final PlayerControl control = new PlayerControl();
        control.setAlignmentY(CENTER_ALIGNMENT);
        super.add(control);
        volumeControl.setAlignmentY(CENTER_ALIGNMENT);
        super.add(volumeControl);

        volumeControl.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
            }
        });

        super.setPreferredSize(MAX_DIMENSION);
        super.setMaximumSize(MAX_DIMENSION);
        super.setMinimumSize(MAX_DIMENSION);
        super.setAlignmentY(CENTER_ALIGNMENT);

    }

    public static void updateButtonText(boolean play) {
        PlayerControl.playButton.setText(play ? "Stop" : "Play");
    }

    public static int getVolumeValue() {
        return volumeControl.getValue();
    }

    /**
     * <code>PlayerControl</code> represents .....
     * 
     * @author ykim
     * @version $Revision: 1.6 $
     * @since 0.1
     */
    private static class PlayerControl extends JPanel {

        private static final JButton prevButton = new JButton("Prev");

        private static final JButton playButton = new JButton("Play");

        private static final JButton nextButton = new JButton("Next");

        /**
         *  
         */
        private PlayerControl() {
            super();
            initialize();
        }

        /**
         *  
         */
        private void initialize() {
            // set layout
            this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

            // action event handlers
            this.addPreviousActionListener();
            this.addPlayActionListener();
            this.addNextActionListener();

            // add to component
            this.add(prevButton);
            this.add(playButton);
            this.add(nextButton);
        }

        /**
         *  
         */
        private void addNextActionListener() {
            nextButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        final boolean playing = PlayerManager
                                .getInstance()
                                .isPlaying();

                        if (playing) {
                            PlayerManager.getInstance().getMediaPlayer().stop();
                        }

                        CollectionManager.getInstance().moveNext();

                        if (playing) {
                            PlayerManager.getInstance().getMediaPlayer().play();
                        }
                    }
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

        /**
         *  
         */
        private void addPreviousActionListener() {
            prevButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    try {
                        final boolean playing = PlayerManager
                                .getInstance()
                                .isPlaying();

                        if (playing) {
                            PlayerManager.getInstance().getMediaPlayer().stop();
                        }

                        CollectionManager.getInstance().movePrevious();

                        if (playing) {
                            PlayerManager.getInstance().getMediaPlayer().play();
                        }
                    }
                    catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            });
        }

        /**
         *  
         */
        private void addPlayActionListener() {
            playButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    stop();
                    play();
                }
            });
        }

        private void stop() {
            try {
                PlayerManager.getInstance().getMediaPlayer().stop();
            }
            catch (PlayerException e1) {
                e1.printStackTrace();
            }
            PlayerManager.getInstance().setPlaying(false);
        }

        private void play() {
            try {
                PlayerManager.getInstance().getMediaPlayer().play();
            }
            catch (PlayerException e1) {
                e1.printStackTrace();
            }
            PlayerManager.getInstance().setPlaying(true);
            jTunes.setLcdPanelText();
        }
    }

    /**
     * <code>VolumeControl</code> represents .....
     * 
     * @author ykim
     * @version $Revision: 1.6 $
     * @since 0.1
     */
    private static class VolumeControl extends JSlider {
    }
}